﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Text;
using MTGenome.MTG;

namespace MTGenome.Gatherer
{
    [Export(typeof(IDatabase)), PartCreationPolicy(CreationPolicy.NonShared)]
    public sealed class Database : DataContext, IDatabase
    {
        [ImportingConstructor]
        public Database(IDatabaseConnection connection)
            : base(connection.Connection)
        {
#if DEBUG
            Log = Console.Out;
#endif
            ObjectTrackingEnabled = !connection.Readonly;

            lock (connection)
            {
                if (connection.Connection.State != ConnectionState.Open)
                {
                    connection.Connection.Open();
                }
            }
        }

        public Table<Card> _cards = null;
        public Table<Expansion> _expansions = null;

        public IQueryable<ICard> Cards
        {
            get { return _cards; }
        }

        public IQueryable<IExpansion> Expansions
        {
            get { return _expansions; }
        }

        public void InsertOnSubmit(ICard card)
        {
            _cards.InsertOnSubmit(card as Card);
        }

        public void InsertOnSubmit(IExpansion expansion)
        {
            _expansions.InsertOnSubmit(expansion as Expansion);
        }
    }
}
